package com.twetec.cms.service.website.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.dao.mapper.website.ArticleModelMapper;
import com.twetec.cms.model.po.website.ArticleModelPO;
import com.twetec.cms.service.website.ArticleModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2021-09-28
 */
@Service
public class ArticleModelServiceImpl extends ServiceImpl<ArticleModelMapper, ArticleModelPO> implements ArticleModelService {

    @Autowired
    private ArticleModelMapper articleModelMapper;

    @Override
    @Transactional
    public Boolean deleteById(Long id, Long modifierId) {
        // 设置更新人
        ArticleModelPO articleModelPO = new ArticleModelPO();
        UpdateWrapper<ArticleModelPO> articleModelPOUpdateWrapper = new UpdateWrapper<>();
        articleModelPOUpdateWrapper.set("MODIFIER_ID", modifierId);
        articleModelPOUpdateWrapper.eq("ID", id);
        int modifierResult = articleModelMapper.update(articleModelPO, articleModelPOUpdateWrapper);
        if (modifierResult <= 0) return false;

        // 设置更新人成功则进行删除操作
        int deleteResult = articleModelMapper.deleteById(id);
        return deleteResult > 0;
    }

    @Override
    @Transactional
    public Boolean deleteByIds(ArrayList<Long> idList, Long modifierId) {
        // 设置更新人
        ArticleModelPO articleModelPO = new ArticleModelPO();
        UpdateWrapper<ArticleModelPO> articleModelPOUpdateWrapper = new UpdateWrapper<>();
        articleModelPOUpdateWrapper.set("MODIFIER_ID", modifierId);
        articleModelPOUpdateWrapper.in("id", idList);
        int modifierResult = articleModelMapper.update(articleModelPO, articleModelPOUpdateWrapper);
        if (modifierResult <= 0) return false;

        // 执行删除
        int deleteResult = articleModelMapper.deleteBatchIds(idList);
        return deleteResult > 0;
    }
}
